﻿using System;

namespace NetTopologySuite.Mathematics
{
	public class MathUtil
	{
		public static double Clamp(double x, double min, double max)
		{
			if (x < min)
			{
				return min;
			}
			if (x > max)
			{
				return max;
			}
			return x;
		}

		public static int Clamp(int x, int min, int max)
		{
			if (x < min)
			{
				return min;
			}
			if (x > max)
			{
				return max;
			}
			return x;
		}

		public static double Log10(double x)
		{
			double num = Math.Log(x);
			if (double.IsInfinity(num))
			{
				return num;
			}
			if (double.IsNaN(num))
			{
				return num;
			}
			return num / MathUtil.LOG10;
		}

		public static int Wrap(int index, int max)
		{
			if (index < 0)
			{
				return max - -index % max;
			}
			return index % max;
		}

		public static double Average(double x1, double x2)
		{
			return (x1 + x2) / 2.0;
		}

		public static double Max(double v1, double v2, double v3)
		{
			double result = v1;
			if (v2 > v1)
			{
				result = v2;
			}
			if (v2 > v3)
			{
				result = v3;
			}
			return result;
		}

		public static double Max(double v1, double v2, double v3, double v4)
		{
			double num = v1;
			if (v2 > num)
			{
				num = v2;
			}
			if (v3 > num)
			{
				num = v3;
			}
			if (v4 > num)
			{
				num = v4;
			}
			return num;
		}

		public static double Min(double v1, double v2, double v3)
		{
			double num = v1;
			if (v2 < num)
			{
				num = v2;
			}
			if (v3 < num)
			{
				num = v3;
			}
			return num;
		}

		public static double Min(double v1, double v2, double v3, double v4)
		{
			double num = v1;
			if (v2 < num)
			{
				num = v2;
			}
			if (v3 < num)
			{
				num = v3;
			}
			if (v4 < num)
			{
				num = v4;
			}
			return num;
		}

		private static readonly double LOG10 = Math.Log(10.0);
	}
}
